package com.aube.views.vote;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.aube.R;
import com.huyn.baseframework.utils.SysUtil;
import com.huyn.baseframework.utils.Utils;

public class VoteProgressBar extends View {

    private static final int DURATION = 300;

    private int conorRadius;

    private int max = 100, progress = 0;

    private float percent;

    private RectF rectF = new RectF();

    private Paint paint;

    private Path path;

    private int space;

    private int progressColor, backgroundColor = Color.TRANSPARENT;

    private boolean fromRight = false;

    private boolean fromLeft = false;

    private boolean isSelected = false;

    public VoteProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public VoteProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public VoteProgressBar(Context context) {
        this(context, null);
    }

    private void init(Context context) {
        conorRadius = context.getResources().getDimensionPixelOffset(R.dimen.option_conor);

        paint = new Paint();
        //paint.setAntiAlias(true);
        //paint.setStyle(Paint.Style.FILL);

        path = new Path();
    }

    public void setSelected(boolean state) {
        this.isSelected = state;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setProgressColor(int color) {
        this.progressColor = color;
    }

    public void setProgressColorRes(int res) {
        setProgressColor(getContext().getResources().getColor(res));
    }

    public void setBackgroundColor(int color) {
        this.backgroundColor = color;
    }

    public void setBackgroundColorRes(int res) {
        setProgressColorRes(getContext().getResources().getColor(res));
    }

    public void setSpace(int space) {
        this.space = space;
    }

    public int getSpace() {
        return space;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public void setProgress(int progress) {
        //if (this.progress != progress)
        {
            this.progress = progress;
            percent = progress / (float) max;
            invalidate();
        }
    }

    public float changePercentFromSpace(float percent, int space) {  //将带有space的percent 换算成正常值
        int viewWidth = getWidth() > 0 ? getWidth() : (SysUtil.getScreenWidth(getContext()) - Utils.dip2px(getContext(), 60));
        int width = viewWidth - (space << 1);
        float progressWidth = percent * width + space;
        return progressWidth / viewWidth;
    }

    private float mFrom = 0, mTo = 0;

    public void setProgress(float from, float to, boolean anim) {
//        if (percent != this.percent)
        {
            if (anim) {
                this.mFrom = from;
                this.mTo = to;
                ValueAnimator vAnim = ValueAnimator.ofFloat(from, to).setDuration(DURATION);
                vAnim.addUpdateListener(new AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float percent = (Float) animation.getAnimatedValue();
                        if (mListener != null)
                            mListener.updateProgress(percent);
                        setProgress(percent);
                    }
                });
                vAnim.addListener(new AnimatorListenerAdapter() {

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (mListener != null)
                            mListener.animEnd();
                    }

                });
                vAnim.start();
            } else {
                setProgress(to);
            }
        }
    }

    public static interface IProgressListener {
        public void updateProgress(float percent);

        public void animEnd();
    }

    public IProgressListener mListener;

    public void setIProgressListener(IProgressListener listener) {
        if (this.mListener == null)
            this.mListener = listener;
    }

    public void setProgress(float percent) {
//        if (percent != this.percent)
        {
            progress = (int) (max * percent);
            this.percent = percent;
            invalidate();
        }
    }

    public float getPercent() {
        return percent;
    }

    public int getProgress() {
        return progress;
    }

    public int getMax() {
        return max;
    }

    public void setRight(boolean right) {
        fromRight = right;
    }

    public void setLeft(boolean left) {
        fromLeft = left;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(backgroundColor);
        rectF.set(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(rectF, conorRadius, conorRadius, paint);

        path.reset();

        int width = getWidth() - (space << 1);
        float progressWidth = percent * width + space;
        if (fromRight) {
            float start = 0;
            if (mFrom == 1 && mTo < 1) {
                start = percent * width + (space * (percent + mFrom - 2 * mTo) / (mFrom - mTo));
            } else if (mTo == 1 && mFrom < 1) {
                start = percent * width + (space * (percent + mTo - 2 * mFrom) / (mTo - mFrom));
            } else if (mTo == 1 && mFrom == 1) {
                if (isSelected)
                    start = getWidth() - space;//防止选中了还是0%的极端情况发生
                else
                    start = getWidth();
            } else {
                start = percent * width + space;
            }

            rectF.set(start, 0, getWidth(), getHeight());
            path.addRoundRect(rectF,
                    new float[]{0, 0, conorRadius, conorRadius, conorRadius, conorRadius, 0, 0},
                    Direction.CW);
        } else if (fromLeft) {
            float end = 0;
            if (mFrom == 0 && mTo > 0) {
                end = percent * width + space * percent / mTo;
            } else if (mTo == 0 && mFrom > 0) {
                end = percent * width + space * percent / mFrom;
            } else if (mTo == 0 && mFrom == 0) {
                if (isSelected)
                    end = space;////防止选中了还是0%的极端情况发生
                else
                    end = 0;
            } else {
                end = percent * width + space;
            }

            rectF.set(0, 0, end, getHeight());
            path.addRoundRect(rectF,
                    new float[]{conorRadius, conorRadius, 0, 0, 0, 0, conorRadius, conorRadius},
                    Direction.CW);
        } else {
            rectF.set(0, 0, progressWidth, getHeight());
            path.addRoundRect(rectF,
                    new float[]{conorRadius, conorRadius, 0, 0, 0, 0, conorRadius, conorRadius},
                    Direction.CW);
        }
        paint.setColor(progressColor);
        canvas.drawPath(path, paint);
    }
}
